package com.zyl.Ecs.auth.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.zyl.Ecs.common.domain.auth.bean.Account;
import com.zyl.Ecs.common.domain.auth.ext.AccountExt;
import com.zyl.Ecs.auth.mapper.*;
import com.zyl.Ecs.auth.service.AuthService;
import com.zyl.Ecs.common.constant.BusinessConstant;
import com.zyl.Ecs.common.exception.EcsException;
import com.zyl.Ecs.common.response.ResultCode;
import com.zyl.Ecs.common.utils.CUtil;
import com.zyl.Ecs.common.utils.JwtUtil;
import com.zyl.Ecs.common.utils.ObjectUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
* @version v1.0
* @Package: com.zyl.Ecs.auth.service.impl
* @ClassName: AccountServiceImpl
* @Description 针对表【base_account】的数据库操作Service实现
* @Author 翟玉龙
* @Date 2024-12-04 16:36:41
*/

@Service
@RequiredArgsConstructor
public class AuthServiceImpl extends MPJBaseServiceImpl<AccountMapper, Account>
    implements AuthService {

    private final AccountMapper accountMapper;
    private final IService iService;

    /**
     * 登录
     * @param username 用户名
     * @param password 密码
     * @return {@link String }
     * @throws EcsException 异常
     */
    @Override
    public String login(String username, String password) throws EcsException{
        /* 根据提交的username查询用户信息 */
        Account account = accountMapper.selectOne(Wrappers.<Account>lambdaQuery()
                .eq(Account::getUsername, username));
        /* 如果根据用户名没有查到结果，抛出异常表示用户名不存在 */
        if (ObjectUtil.isNull(account)) {
            CUtil.logInfo("用户名不存在: " + username);
            throw new EcsException(ResultCode.LOGIN_USERNAME_INVALID);
        }
        CUtil.logInfo(account.toString());
        /* 判断密码是否正确 */
        if (ObjectUtil.notEquals(CUtil.encrypt(password), account.getPassword())) {
            CUtil.logInfo("密码错误: " + password);
            throw new EcsException(ResultCode.LOGIN_PASSWORD_INVALID);
        }

        /* 判断用户状态是否正常 */
        if (ObjectUtil.equals(account.getStatus(),
                BusinessConstant.ACCOUNT_STATUS_FORBIDDEN)) {
            CUtil.logInfo("用户被禁用: " + username);
            throw new EcsException(ResultCode.LOGIN_STATUS_INVALID);
        }

        /* 用户ID，写入到Token的固定载荷字段Audience */
        Integer accountId = account.getId();
        CUtil.logInfo("用户ID" + accountId);

        /* 其余需要保存的用户信息，封装为一个Map集合 */
        Map<String, Object> claims = new HashMap<>();
        claims.put("username", account.getUsername());
        claims.put("telephone", account.getTelephone());

        /* 使用用户信息签发Token令牌 */
        return JwtUtil.sign(String.valueOf(accountId), claims);
    }

    /**
     * 用户信息
     * @param token token数据
     * @return {@link AccountExt }
     * @throws EcsException 异常
     */
    @Override
    public AccountExt userInfo(String token) throws EcsException{
        /* 从Token中解析出用户ID */
        String accountId = JwtUtil.getAccountId(token);

        if (ObjectUtil.notHasText(accountId)) {
            throw new EcsException(ResultCode.TOKEN_INVALID);
        }

        /* 查询用户信息 */
        Account account = accountMapper.selectById(accountId);

        if (ObjectUtil.isNull(account)) {
            throw new EcsException(ResultCode.ACCOUNT_NOT_EXIST);
        }

        /* 用户信息转Ext类型 */
        AccountExt accountExt = AccountExt.toExt(account);

        /* 设置用户角色和权限 */
        return iService.fillRoles(accountExt, Integer.parseInt(accountId), false);
    }
}




